package org.gpslw.util;


import lombok.extern.slf4j.Slf4j;
import org.geotools.api.feature.simple.SimpleFeature;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.store.ContentFeatureCollection;
import org.geotools.data.store.ContentFeatureSource;
import org.geotools.data.wfs.WFSDataStore;
import org.geotools.data.wfs.WFSDataStoreFactory;
import org.gpslw.supermap.SuperMapWfsDataStoreFactory;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class WfsUtils {

    public static Map<String, List<SimpleFeature>> readWfsFile(String wfsUrl) {
        Map<String, List<SimpleFeature>> result =  new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        try {
            params.put(WFSDataStoreFactory.URL.key, new URL(wfsUrl));
            WFSDataStoreFactory factory = new WFSDataStoreFactory();
            WFSDataStore dataStore = factory.createDataStore(params);
            //可能设置多个图层
            String[] typeNames = dataStore.getTypeNames();
            for (String typeName : typeNames) {
                ArrayList<SimpleFeature> typeFeatures = new ArrayList<>();
                ContentFeatureSource featureSource = dataStore.getFeatureSource(typeName);
                ContentFeatureCollection featureCollection = featureSource.getFeatures();
                SimpleFeatureIterator fIterator = featureCollection.features();
                while (fIterator.hasNext()) {
                    SimpleFeature feature = fIterator.next();
                    typeFeatures.add(feature);
                }
                result.put(typeName, typeFeatures);
                fIterator.close();
            }
            dataStore.dispose();
        } catch (IOException e) {
            log.info("DataStoreUtils getDataStore error : {}", e.getMessage());
            throw new RuntimeException(e);
        }
        return result;
    }

    public static Map<String, List<SimpleFeature>> readSuperMapWfs(String wfsUrl) {
        Map<String, List<SimpleFeature>> result =  new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        try {
            params.put(WFSDataStoreFactory.URL.key, new URL(wfsUrl));
            WFSDataStoreFactory factory = new SuperMapWfsDataStoreFactory();
            WFSDataStore dataStore = factory.createDataStore(params);
            //可能设置多个图层
            String[] typeNames = dataStore.getTypeNames();
            for (String typeName : typeNames) {
                ArrayList<SimpleFeature> typeFeatures = new ArrayList<>();
                ContentFeatureSource featureSource = dataStore.getFeatureSource(typeName);
                ContentFeatureCollection featureCollection = featureSource.getFeatures();
                SimpleFeatureIterator fIterator = featureCollection.features();
                while (fIterator.hasNext()) {
                    SimpleFeature feature = fIterator.next();
                    typeFeatures.add(feature);
                }
                result.put(typeName, typeFeatures);
                fIterator.close();
            }
            dataStore.dispose();
        } catch (IOException e) {
            log.info("DataStoreUtils getDataStore error : {}", e.getMessage());
            throw new RuntimeException(e);
        }
        return result;
    }
}
